<?php

$database = 'dart';
$mysqli = new mysqli("localhost", "root", "hanswurst0815", $database);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (isset($_GET['insert']) == false && isset($_GET['update']) == false && isset($_GET['delete']) == false) 
{
	$res = $mysqli->query("SELECT id,firstName,lastName,email FROM players");

	for ($row_no = $res->num_rows - 1; $row_no >= 0; $row_no--) {
		$res->data_seek($row_no);
		$row = $res->fetch_assoc();
		$players[] = array(
			'id' => $row['id'],
			'firstName' => $row['firstName'],
			'lastName' => $row['lastName'],
			'email' => $row['email']
			);
	}
	echo json_encode($players);
	//error_log(json_encode($players), 0);
}

if (isset($_GET['insert']))
{
    // INSERT COMMAND
    $insert_query = "INSERT INTO players() VALUES()";
	//error_log(json_encode($insert_query), 0);
	$res = $mysqli->query($insert_query);
	echo $res;
}

if (isset($_GET['update']))
{
    // UPDATE COMMAND
    $update_query = "UPDATE players SET firstName='".$_GET['firstName']."',lastName='".$_GET['lastName']."',email='".$_GET['email']."' WHERE id=".$_GET['id'];
    $res = $mysqli->query($update_query);
	//error_log($update_query, 0);
    echo $res;
}

if (isset($_GET['delete']))
{
    // DELETE COMMAND
    $delete_query = "DELETE FROM players WHERE id=".$_GET['id'];
	//error_log($delete_query, 0);
    $res = $mysqli->query($delete_query);
    echo $res;
}
